import React from 'react';
import {Breadcrumb,  Button, Table, Input, Select, Form, Pagination, message, } from 'antd';
import $ from 'jquery';
import Config from '../../common/config';
import City from '../../common/city';
import { hashHistory} from 'react-router';
import moment from 'moment';
const FormItem = Form.Item;
const config = new Config();
const Option = Select.Option;
const InputGroup = Input.Group;
const token = sessionStorage.getItem('token');
class ChooseIndexDoctor1 extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            gender:undefined,//性别
            dorRole:undefined,//角色
            deptId:undefined,//科室
            job:undefined,//职称
            province:undefined,//省
            city:undefined,//市
            region:undefined,//区
            t_citys:[],//市s
            regions:[],//区s
            name:'',//姓名
            goodAt:'',//擅长方向
            productlist: [],//全部商品列表
            productcount: 0,//全部商品条数
            pagesize: 10,// 每页显示的页数
            pageindex: 1,// 当前页
            selectedRows:{},//选择的行
            depList:[],//科室列表
            selectedRowKeys:[],
        }
    }
    componentDidMount() {
        console.log(this.props.location.state);
        this.GetProguctList();
    }
    //商品列表
    GetProguctList() {
        let {gender, dorRole, deptId, job, province, city,  region,name, goodAt, pagesize,pageindex,}=this.state;
        $.ajax({
            url: `${config.integratedBack2}/inte/findDoctorList`,
            type: "POST",
            headers: {
                'Authorization': 'Bearer ' + token,
            },
            data: {
                gender,
                dorRole,
                deptId,
                job,
                province,
                city,
                region,
                name,
                goodAt,
                pagesize,
                pageindex,
                attestation:3,
            },
            success: res => {
                console.log('商品列表',JSON.stringify(res) );
                if (res.code === 200) {
                    res.result.forEach((item, index) => {
                        item.key = index + 1
                    });
                    this.setState({
                        productlist: res.result,
                        productcount: res.pageCounts?res.pageCounts:0,
                        depList:res.depList,
                    })
                }else{
                    message.info(res.msg,1);
                    this.setState({
                        productlist: [],
                        productcount: 0,
                        depList:[],
                    });
                }
            },
            error:err=>{
                console.log(err);
            }
        });
    }
    //搜索
    SearchProductListBtn() {
        const form = this.props.form;
        this.setState({
            name: form.getFieldValue('name') ? form.getFieldValue('name') : '',
            goodAt: form.getFieldValue('goodAt') ? form.getFieldValue('goodAt') : '',
            selectedRowKeys:[],
            selectedRows:{},
        }, () => {
            this.GetProguctList();
        });
    }
    //重置
    ResetBtn() {
        const form = this.props.form;
        form.resetFields();
        this.setState({
            pageindex: 1,
            gender:undefined,//性别
            dorRole:undefined,//角色
            deptId:undefined,//科室
            job:undefined,//职称
            province:undefined,//省
            city:undefined,//市
            region:undefined,//
            name:'',//姓名
            goodAt:'',//擅长方向
            selectedRowKeys:[],
            selectedRows:{},
        }, () => {
            this.GetProguctList();
        });
    }
    // 分页
    changePage(pageindex) {
        this.setState({
            pageindex,
            selectedRowKeys:[],
            selectedRows:{},
        }, () => {
            this.GetProguctList();
        });
    }
    //筛选
    handleValueChange(status,value){
        console.log(City);
        let data={};
        if(status===1){
            data.gender=value;
        }else if(status===2){
            data.dorRole=value;
            this.setState({
                job:undefined
            })
        }else if(status===3){
            data.deptId=value;
        }else if(status===4){
            data.job=value;
        }else if(status===5){
            data.province=value;
            City.forEach(item=>{
                if(item.name===value){
                    this.setState({
                        t_citys:item.child,
                        city:undefined,
                        region:undefined,
                    })
                }
            })
        }else if(status===6){
            data.city=value;
            this.state.t_citys.forEach(item=>{
                if(item.name===value){
                    this.setState({
                        regions:item.child?item.child:[],
                        region:undefined,
                    })
                }
            })
        }else if(status===7){
            data.region=value;
        }
        this.setState(data);
    }
    //保存
    handleSave(){
        if(!(this.state.selectedRows.id)){
            message.error('请先选择一位医生！',1);
            return;
        }
        if((this.state.selectedRows.onstate==1)){
            message.error('此医生已经停用，无法替换，请重新选择！',1);
            return;
        }
        $.ajax({
            url: `${config.integratedBack2}/inte/selectDoctor`,
            type: "GET",
            headers: {
                'Authorization': 'Bearer ' + token,
            },
            data: {
                id:this.props.location.state,
                nowId:this.state.selectedRows.id,
            },
            success: res => {
                if(res.code===200){
                    message.info('更新成功！',1);
                    hashHistory.push('indexDoctor');
                }else{
                    message.info(res.msg,1);
                }
            },
            error:err=>{
                console.log(err);
                message.info('更新失败，请重新操作！',1);
            }
        });
    }
    //取消保存
    handleCancel(){
        message.info('取消操作！',1);
        hashHistory.push('indexDoctor');
    }
    render() {
        const { getFieldDecorator } = this.props.form;
        let {selectedRowKeys}=this.state;
        const rowSelection = {
            type:"radio",
            selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
                console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows,);
                this.setState({
                    selectedRows:selectedRows[0],
                    selectedRowKeys,
                });
            },
        };
        const columns = [
            {
                title: '序号',
                dataIndex: 'key',
            },
            {
                title: '医生ID',
                dataIndex: 'id',
            },
            {
                title: '手机',
                dataIndex: 'phone',
            },
            {
                title: '姓名',
                dataIndex: 'name',
            },
            {
                title: '角色',
                dataIndex: 'dorRole',
            },
            {
                title: '职称',
                dataIndex: 'job',
            },
            {
                title: '性别',
                dataIndex: 'gender',
                render:text=><span>{text==1?'男':text==2?'女':'不详'}</span>
            },
            {
                title: '科室',
                dataIndex: 'depName',
            },
            {
                title: '出生日期',
                dataIndex: 'birthday',
            },
            {
                title: '擅长方向',
                dataIndex: 'goodAt',
                render:text=><span>{text?text.slice(0,10)+'...':''}</span>
            },
            {
                title: '省',
                dataIndex: 'province',
            },
            {
                title: '市',
                dataIndex: 'city',
            },
            {
                title: '区',
                dataIndex: 'region',

            },
            {
                title: '来源',
                dataIndex: 'source',
            },
            {
                title: '认证',
                dataIndex: 'attestation',
                render:text=><span style={{color:'#469946'}}>{text==0?'未认证':text==1?'申请中':text==3?'已认证':text==2?'未通过':''}</span>
            },
            {
                title: '状态',
                dataIndex: 'onstate',
                render:text=><span style={{color:text==0?'':'red'}}>{text==0?'正常':'禁用'}</span>
            },
            {
                title: '注册时间',
                dataIndex: 'createdAt',
                render:text=><span>{text?moment(text).format('YYYY-MM-DD HH:mm:ss'):''}</span>
            },
            {
                title: '最后登录时间',
                dataIndex: 't_endtime',
            },
        ];
        return (
            <Form>
                <div className='goods-label' style={{ minWidth: 1250 }}>
                    <div className='goods-label-cont clearfix'>
                        <div className='goods-label-cont-nav clearfix'>
                            <Breadcrumb separator=">">
                                <Breadcrumb.Item>综合平台</Breadcrumb.Item>
                                <Breadcrumb.Item><span className='separator-a'>选择首页医生</span></Breadcrumb.Item>
                            </Breadcrumb>
                        </div>

                        <div className='member-content-Search clearfix'>
                            <div className='addetd'>
                                <FormItem>
                                    {getFieldDecorator('name')(
                                            <Input className='addetd-a-r' addonBefore="医生姓名" placeholder="请输入" />
                                        )
                                    }
                                </FormItem>
                            </div>
                            <div className='addetd addetd-a indtner'>
                                <InputGroup compact>
                                    <Input addonBefore="性 别" />
                                    <Select placeholder='请选择' value={this.state.gender} onChange={this.handleValueChange.bind(this,1)}>
                                        <Option value="">全部</Option>
                                        <Option value="1">男</Option>
                                        <Option value="2">女</Option>
                                        <Option value="0">未知</Option>
                                    </Select>
                                </InputGroup>
                            </div>
                            <div className='addetd addetd-a indtner'>
                                <InputGroup compact>
                                    <Input addonBefore="角 色" />
                                    <Select placeholder='请选择' value={this.state.dorRole} onChange={this.handleValueChange.bind(this,2)}>
                                        <Option value="">全部</Option>
                                        <Option value="医生">医生</Option>
                                        <Option value="药师">药师</Option>
                                    </Select>
                                </InputGroup>
                            </div>
                            <div className='addetd addetd-a indtner'>
                                <InputGroup compact>
                                    <Input addonBefore="科 室" />
                                    <Select placeholder='请选择' value={this.state.deptId} onChange={this.handleValueChange.bind(this,3)}>
                                        <Option value="">全部</Option>
                                        {this.state.depList.map(item=><Option value={item.id.toString()} key={item.id}>{item.name}</Option>)}
                                    </Select>
                                </InputGroup>
                            </div>
                            <div className='addetd addetd-a indtner'>
                                <InputGroup compact>
                                    <Input addonBefore="职 称" />
                                    <Select placeholder='请选择'  value={this.state.job} onChange={this.handleValueChange.bind(this,4)} >
                                        <Option value="">全部</Option>
                                        {this.state.dorRole?
                                            (this.state.dorRole==="医生"?
                                                ["医师" ,"主治医师","副主任医师","主任医师"].map(item=> <Option value={item} key={item}>{item}</Option>)
                                                :["药士","药师","主管药师","副主任药师","主任药师"].map(item=> <Option value={item} key={item}>{item}</Option>)):''}
                                    </Select>
                                </InputGroup>
                            </div>
                        </div>
                        <div className='member-content-Search clearfix'>
                            <div className='addetd'>
                                <FormItem>
                                    {
                                        getFieldDecorator('goodAt')(
                                            <Input className='addetd-a-r' addonBefore="擅长方向" placeholder="请输入" />
                                        )
                                    }
                                </FormItem>
                            </div>
                            <div className='addetd addetd-a indtner'>
                                <InputGroup compact>
                                    <Input addonBefore="选 省" />
                                    <Select value={this.state.province} onChange={this.handleValueChange.bind(this,5)} placeholder='请选择'>
                                        <Option value="">全部</Option>
                                        {City.map(item=> <Option value={item.name} key={item.code}>{item.name}</Option>)}
                                    </Select>
                                </InputGroup>
                            </div>
                            <div className='addetd addetd-a indtner'>
                                <InputGroup compact>
                                    <Input addonBefore="选 市" />
                                    <Select value={this.state.city} onChange={this.handleValueChange.bind(this,6)} placeholder='请选择'>
                                        <Option value="">全部</Option>
                                        {this.state.t_citys.map(item=> <Option value={item.name} key={item.code}>{item.name}</Option>)}
                                    </Select>
                                </InputGroup>
                            </div>
                            <div className='addetd addetd-a indtner'>
                                <InputGroup compact>
                                    <Input addonBefore="选 区" />
                                    <Select value={this.state.region} onChange={this.handleValueChange.bind(this,7)} placeholder='请选择'>
                                        <Option value="">全部</Option>
                                        {this.state.regions.map(item=> <Option value={item.name} key={item.code}>{item.name}</Option>)}
                                    </Select>
                                </InputGroup>
                            </div>
                            <Button type="primary" className='member-button' icon="search" onClick={this.SearchProductListBtn.bind(this)}>搜索</Button>
                            <Button type="primary" className='member-button' icon="reload" onClick={this.ResetBtn.bind(this)}>重置</Button>
                        </div>
                        <div className='clearfix'>
                            <Table rowSelection={rowSelection}
                                   scroll={{ x: 100 }}
                                   rowKey="key"
                                   columns={columns}
                                   dataSource={this.state.productlist}
                                   pagination={false}
                                   bordered
                                   className='allgoods-table'
                            />
                            <div className='goods-label-tabletr' style={{textAlign:'center'}}>
                                <Button className='goods-label-table-app' width={105}  type="primary" style={{color:'white'}} onClick={this.handleSave.bind(this)}>保存</Button>
                                <Button className='goods-label-table-app' width={105}  onClick={this.handleCancel.bind(this)}>取消</Button>
                            </div>
                        </div>
                        <div className='goods-label-let'>
                            <div className="floor-ppagi">
                                {
                                    this.state.productcount == 0 ?
                                        <p className="floor-ppagip">
                                            共{this.state.productcount}条记录
                                        </p> :
                                        <p className="floor-ppagip">
                                            当前显示
                                            {this.state.pagesize * (this.state.pageindex - 1) + 1}—
                                            {this.state.pagesize * this.state.pageindex < this.state.productcount ? this.state.pagesize * this.state.pageindex : this.state.productcount}条记录，
                                            共{this.state.productcount}条记录
                                        </p>
                                }
                                <Pagination current={this.state.pageindex} total={this.state.productcount || 1} defaultPageSize={this.state.pagesize} onChange={this.changePage.bind(this)} />
                            </div>
                        </div>
                    </div>
                </div>
            </Form>
        )
    }
}

const ChooseIndexDoctor = Form.create()(ChooseIndexDoctor1);
export default ChooseIndexDoctor;